package com.zhouyu.mysql.core;

import com.zhouyu.mysql.MysqlServer;
import com.zhouyu.mysql.dict.SystemDict;
import com.zhouyu.mysql.page.IndexPage;
import com.zhouyu.mysql.page.Page;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

/**
 * 大都督周瑜（我的微信: dadudu6789）
 */
public class PageUtil {

    public static final int PAGE_SIZE = 16 * 1024;  // 16KB

    public static int createPage(int spaceId) {
        int pageNo = SpaceUtil.getNextPageNo(spaceId);

        ByteBuffer byteBuffer = ByteBuffer.allocate(PAGE_SIZE);
        IndexPage page = new IndexPage();
        page.setPageByteBuffer(byteBuffer);
        page.fil_page_set_space_id(spaceId);
        page.fil_page_set_page_offset(pageNo);
        page.fil_page_set_type(17855);   // 源码中17855表示INDEX页，暂时还不知道怎么要用这个数字

        page.init_file_header(spaceId, pageNo);
        page.init_page_header();

        return flushPage(page);
    }

    public static int flushPage(Page page) {
        int spaceId = page.fil_page_get_space_id();
        int pageNo = page.fil_page_get_page_offset();
        Path path = SpaceUtil.getPathBySpaceId(spaceId);
        ByteBuffer byteBuffer = page.getPageByteBuffer();

        try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.WRITE)) {
            page.pageByteBuffer.clear(); // 确保position在ByteBuffer的起始位
            fileChannel.position((long) pageNo * PAGE_SIZE);
            return fileChannel.write(byteBuffer);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws Exception {
        SystemDict.getInstance().deserialize();
        createPage(1);
    }

}
